/*
 * Delay3Description.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */
package engine;

import java.util.Vector;
import java.io.Serializable;

/**
 * Used as a structure to store a Delay3
 * Unless the standard, this is more permissive (i.e any constant expression is allowed).
 * Also note that if some values are undefined, they are treated as 0 (no exceptions thrown).
 */

@SuppressWarnings("serial")
public class Delay3Description implements Serializable{
	/*
	 * This class (and Delay3 also) is used as a C struct thing,
	 * and it is explicitely modified.
	 */


	/**
	 * used as default delay
	 */
	static Delay3Description nullDelay = new Delay3Description();
	static{
		nullDelay.delaySpecs = 1;
		nullDelay.delay1Min = nullDelay.delay1Typ =
			nullDelay.delay1Max = ConstantDescription.zeroDescription;
	}


	public int delaySpecs; //how many delays below are valid
	public ExpressionDescription delay1Typ, delay2Typ, delay3Typ;
	public ExpressionDescription delay1Min, delay2Min, delay3Min;
	public ExpressionDescription delay1Max, delay2Max, delay3Max;

	public Delay3 instantiate(NameSpace ns)
	throws ParseException, InterpretTimeException
	{
		Delay3 res = new Delay3();

		res.delaySpecs = delaySpecs;

		res.delay1Typ = delay1Typ != null?
				(int) delay1Typ.instantiate(ns).evaluate().getLong() : 0;
				res.delay2Typ = delay2Typ != null?
						(int) delay2Typ.instantiate(ns).evaluate().getLong() : 0;
						res.delay3Typ = delay3Typ != null?
								(int) delay3Typ.instantiate(ns).evaluate().getLong() : 0;

								res.delay1Min = delay1Min != null?
										(int) delay1Min.instantiate(ns).evaluate().getLong() : 0;
										res.delay2Min = delay2Min != null?
												(int) delay2Min.instantiate(ns).evaluate().getLong() : 0;
												res.delay3Min = delay3Min != null?
														(int) delay3Min.instantiate(ns).evaluate().getLong() : 0;

														res.delay1Max = delay1Max != null?
																(int) delay1Max.instantiate(ns).evaluate().getLong() : 0;
																res.delay2Max = delay2Max != null?
																		(int) delay2Max.instantiate(ns).evaluate().getLong() : 0;
																		res.delay3Max = delay3Max != null?
																				(int) delay3Max.instantiate(ns).evaluate().getLong() : 0;

																				return res;
	}

	/**
	 * Move the ExpDescs from values to the apropriate place inside here
	 * @param position is the delay# to be set up here
	 * @param values supposed to hold 1/2/3 values for the min/typ/max delay
	 */
	public void copyDelays(Vector values, int position){
		switch(position){
		case 1:
			if(values.size() == 1)
				delay1Typ = (ExpressionDescription) values.elementAt(0);
			else{
				delay1Min = (ExpressionDescription)  values.elementAt(0);
				delay1Typ = (ExpressionDescription) values.elementAt(1);
				delay1Max = (ExpressionDescription) values.elementAt(2);
			}
			break;
		case 2:
			if(values.size() == 1)
				delay2Typ = (ExpressionDescription) values.elementAt(0);
			else{
				delay2Min = (ExpressionDescription) values.elementAt(0);
				delay2Typ = (ExpressionDescription) values.elementAt(1);
				delay2Max = (ExpressionDescription) values.elementAt(2);
			}
			break;
		case 3:
			if(values.size() == 1)
				delay3Typ = (ExpressionDescription) values.elementAt(0);
			else{
				delay3Min = (ExpressionDescription) values.elementAt(0);
				delay3Typ = (ExpressionDescription) values.elementAt(1);
				delay3Max = (ExpressionDescription) values.elementAt(2);
			}
		}
	}

	/**
	 * Copies the values as rising-fallinf-turnoff
	 */
	public void copyDelays(Vector values) {
		switch (delaySpecs = values.size()) {
		case 3: delay3Typ = (ExpressionDescription) values.elementAt(2);
		case 2: delay2Typ = (ExpressionDescription) values.elementAt(1);
		case 1: delay1Typ = (ExpressionDescription) values.elementAt(0);
		}
	}

	public String toString() {
		String res = "#(";
		switch (delaySpecs) {
		case 3: 
			res += delay3Min + ":" + delay3Typ + ":" + delay3Max + ", ";
		case 2:
			res += delay2Min + ":" + delay2Typ + ":" + delay2Max + ", ";
		case 1:
			res += delay1Min + ":" + delay1Typ + ":" + delay1Max;
		}
		return res + ")";
	}
}
